/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AnalysisDialog.java
 *
 * Created on 11.08.2010, 11:53:22
 */
package ru.ibrae.mchs.radiation.GUI;

import java.util.Arrays;
import ru.ibrae.mchs.radiation.common.reports.ReportsCreator;
import ru.ibrae.mchs.radiation.common.reports.ReportType;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import jxl.read.biff.BiffException;
import jxl.write.WriteException;

/**
 *
 * @author murr
 */
public class AnalysisDialog extends javax.swing.JFrame {

    /** Creates new form AnalysisDialog */
    public AnalysisDialog() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jComboBox1 = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jButton1.setText("Проект перечня зонирования");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N

        jButton2.setText("Отчет по зонированию");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 841, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jComboBox1, 0, 591, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        final Loading wait = new Loading(this, false);
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    ReportsCreator.createReport(ReportType.FULL);
                } catch (IOException ex) {
                    Logger.getLogger(AnalysisDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (WriteException ex) {
                    Logger.getLogger(AnalysisDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (BiffException ex) {
                    Logger.getLogger(AnalysisDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(AnalysisDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
                wait.close();
            }
        };
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        // TODO add your handling code here:
        File zonageDir = new File("templates/zonage");
        File[] listFiles = zonageDir.listFiles(new FileFilter() {

            @Override
            public boolean accept(File pathname) {
                return pathname.getAbsolutePath().contains(".xls");
            }
        });
        jComboBox1.removeAllItems();
        ArrayList<File> files = new ArrayList<File>();
        boolean addAll = files.addAll(Arrays.asList(listFiles));
        Collections.sort(files, new Comparator<File>() {

            @Override
            public int compare(File o1, File o2) {
                return o1.getName().compareToIgnoreCase(o2.getName());
            }
        });
        for (File file : files) {
            jComboBox1.addItem(file.getName());
        }
    }//GEN-LAST:event_formWindowOpened

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        final Loading wait = new Loading(this, false);
        Runnable r = new Runnable() {

            @Override
            public void run() {
                try {
                    if (jComboBox1.getSelectedItem().toString().contains("действующее")) {
                        if (jComboBox1.getSelectedItem().toString().contains("НАСЕЛЕННЫЕ")) {
                            if (jComboBox1.getSelectedItem().toString().contains("Упраздненные")) {

                                ReportsCreator.createReport(ReportType.ZONAGE_ACTIVE_REMOVED);

                            } else {
                                ReportsCreator.createReport(ReportType.ZONAGE_ACTIVE_NP);
                            }
                        } else {
                            ReportsCreator.createReport(ReportType.ZONAGE_ACTIVE_ALL);
                        }
                    } else {
                        if (jComboBox1.getSelectedItem().toString().contains("НАСЕЛЕННЫЕ")) {
                            if (jComboBox1.getSelectedItem().toString().contains("Упраздненные")) {
                                ReportsCreator.createReport(ReportType.ZONAGE_PLANNED_REMOVED);
                            } else {
                                ReportsCreator.createReport(ReportType.ZONAGE_PLANNED_NP);
                            }
                        } else {
                            ReportsCreator.createReport(ReportType.ZONAGE_PLANNED_ALL);
                        }
                    }
                } catch (IOException ex) {
                    Logger.getLogger(AnalysisDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (WriteException ex) {
                    Logger.getLogger(AnalysisDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (BiffException ex) {
                    Logger.getLogger(AnalysisDialog.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(AnalysisDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
                wait.close();
            }
        };
        SwingUtilities.invokeLater(r);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AnalysisDialog().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    // End of variables declaration//GEN-END:variables
}
